/*
 * @Author: Gary
 * @Date: 2018-09-26 14:43:02
 * @Last Modified by: Gary
 * @Last Modified time: 2018-10-09 16:41:53
 * 编辑新增页刷新判断
 * <pub ref="pub"></pub>
 * 并在pub中内置refresh方法用以刷新所做操作，当判断为需要刷新时，会调用子组件的该方法
 */
import Store from '@/store'
import qs from 'qs'

const editRefresh = {
  beforeRouteEnter(to, from, next) {
    let tagList = Store.getters.cachedViews, routeName = to.name, flag = true;
    for (let i = 0; i < tagList.length; i++) {
      if (tagList[i] == routeName) {
        flag = false;
      }
    }
    next(vm => {
      vm.reload = flag;
    })
  },
  data() {
    return {
      reload: true,
      queryData: qs.stringify(this.$route.query)
    }
  },
  activated() {
    console.log(this.$route);
    // debugger
    setTimeout(() => {
      const queryJson = qs.stringify(this.$route.query);
      if (this.reload) {
        this.$refs.pub && this.$refs.pub.refresh();
        this.refresh && this.refresh();
        console.log('需要刷新页面-新开tag')
      } else if (queryJson != this.queryData) {
        this.queryData = queryJson;
        this.$refs.pub && this.$refs.pub.refresh();
        this.refresh && this.refresh();
        console.log('需要刷新页面-参数变化')
      }
    })
  }
}

export default editRefresh